# -*- coding: utf-8 -*-
import wsgiref.handlers
from google.appengine.ext import webapp
import xmw
from xmw import error
from xmw import tianya
import logging
from google.appengine.ext import db
import datetime


class MainPage(xmw.RequestHandler):
    def get_thread2(self, status):
        """
        q = tianya.TianyaThread.all()
        q.filter('status = ', status)
        q.filter(' hot > ',0)
        thread = q.get()
        """
        q = db.GqlQuery("SELECT * FROM TianyaThread"+
                        " WHERE status =:1 "+
                        "AND hot> :2 "+
                        "order by hot DESC",
                        status,-1)
        lst_thread=q.fetch(1)
        if lst_thread is None:
            return None
        if len(lst_thread) ==0:
            return None
        
        return lst_thread[0]
    
    def get_thread(self,status):
        thread=None
        lst_level=tianya.thread_level.keys()
        for level in lst_level:
            delta_seconds= tianya.thread_level[level]
            delta=datetime.timedelta(seconds=delta_seconds)
            now =   xmw.get_cn_now_time() 
            target=now - delta
            logging.info('query thread status:%s,level:%s,target time:%s'%(status,level,str(target)))
            q = db.GqlQuery("SELECT * FROM TianyaThread"+
                        " WHERE status =:1 "+
                        #"AND hot> :2 "+
                        "AND level = :2 "+
                        "AND last_refresh < :3 "+
                        "order by last_refresh ASC",
                        #status,-1,level,target)
                        status,level,target)
            q_count=q.count()
            logging.info('got %d thread'%q_count)
            lst_thread=q.fetch(1)
            if lst_thread is None:
                continue
            if len(lst_thread) ==0:
                continue
        
            thread = lst_thread[0]
            logging.info('got thread: thread_id:%s\ntitle:%s\nlevel:%s\nlastrefresh:%s'%(thread.thread_id,thread.title,thread.level,thread.last_refresh))
            break
        logging.info('got thread:%s'%str(thread))
        return thread
        
    def run_get(self):
        """
        Task1:update INIT thread to RUN
        Task2:on RUN thread,update the page_cnt,url_list,etc.
        """
        get_thread=False        
        thread = self.get_thread(xmw.Thread_Status.RUN)
        
        page=None
        if thread is not None:
            try:
                page=thread.get_offset_page()
                get_thread=True
                thread.schedule()
            except:
                #on exception,dec the hot and return
                thread.hot=thread.hot-1
                thread.last_refresh=xmw.get_cn_now_time()
                thread.schedule()
                #thread.put()
                return get_thread
            
            #page.thread_id=thread.thread_id
        else:
            e=error.OK("no thread needs update")
            self.render_error(e)
            return get_thread
        
        latest_post_date    =   thread.latest_post_date
        #logging.info("the thread_id:%s"%(page.thread_id))
        lst_post    =   page.get_lst_post()
        length=len(lst_post)
        logging.info("read %d posts"%length)
        put_cnt=0
        
        p_cnt=0
        for post in lst_post:
            p_cnt=p_cnt+1
            post_date   =   post.date
            
            is_new=False
            if post_date > latest_post_date :
                #post.put()
                post.save()
                #self.send_msg(post)
                put_cnt=put_cnt+1
                thread.latest_post_date=post_date
                thread.hot=thread.hot+5
                thread.put()
                is_new=True
                
            #just log on is ok
            if p_cnt==length:
                logging.info('the last post date:%s\n is_new :%s'%(post_date,str(is_new)))
                
        thread.on_page_finish(page,put_cnt)
        e=error.OK("thread id:%s,offset:%d,read post:%d,put post %d,latest_post_date:%s"%(thread.thread_id,thread.offset,len(lst_post),put_cnt,str(thread.latest_post_date)))
        self.render_error(e)
        return get_thread
    
    def get(self):
        a=datetime.datetime.now()
        get_thread=self.run_get()
        cnt=1
        while True and get_thread==True:
            b=datetime.datetime.now()
            c=b-a
            if c.seconds <3:
                get_thread=self.run_get()
                cnt=cnt+1
            else:
                break
        b=datetime.datetime.now()
        c=b-a
        logging.info('time cost: %s\n count:%d'%(str(c),cnt))
def main():
    application = webapp.WSGIApplication(
                                       [('/robot/page_daemon', MainPage)],
                                       debug=False)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
    main()